package com.founder.gateway.system.thread;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.setu.framework.core.log.SetuLog;
import org.setu.framework.core.properties.SetuSystemUtil;
import org.setu.framework.jdbc.develop.util.MySQLJdbcUtil;
import org.setu.framework.log.AbstractSetuLogExecutor;
import org.setu.framework.log.SetuLogPack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class SetuLogExecutor extends AbstractSetuLogExecutor {
    protected transient static final Logger logger = LoggerFactory.getLogger(SetuLogExecutor.class.getName());

    public SetuLogExecutor() {
    }

    public SetuLogExecutor(String logPackType, SetuLogPack logPack) {
        super(logPackType, logPack);
    }

    @Override
    public void run() {
        if (logPack != null && logPack.size() > 0) {
            Connection conn = MySQLJdbcUtil.getConnection(SetuSystemUtil.properties.getProperty("url"), SetuSystemUtil.properties.getProperty("username"), SetuSystemUtil.properties.getProperty("password"));
            try {
                SetuLog log;
                while (!logPack.isEmpty()) {
                    log = logPack.poll();
                    SetuApiGatewayLog setuLog = (SetuApiGatewayLog) log;
                    String sql = "INSERT INTO `api_gateway_log`(`id`, `appId`, `userId`, `userName`, `orgCode`, `orgName`, `startTime`, `endTime`, `apiSchemeId`, `uri`, `params`, `sql`, `remoteIp`, `errorCode`, `exception`, `resultType`, `xtLrsj`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                    PreparedStatement pstmt = conn.prepareStatement(sql);
                    pstmt.setString(1, setuLog.getId());
                    pstmt.setString(2, setuLog.getAppId());
                    pstmt.setString(3, setuLog.getUserId());
                    pstmt.setString(4, setuLog.getUserName());
                    pstmt.setString(5, setuLog.getOrgCode());
                    pstmt.setString(6, setuLog.getOrgName());
                    pstmt.setString(7, setuLog.getStartTime());
                    pstmt.setString(8, setuLog.getEndTime());
                    pstmt.setString(9, setuLog.getApiSchemeId());
                    pstmt.setString(10, setuLog.getUri());
                    pstmt.setString(11, setuLog.getParams());
                    pstmt.setString(12, setuLog.getSql());
                    pstmt.setString(13, setuLog.getRemoteIp());
                    pstmt.setString(14, setuLog.getErrorCode());
                    pstmt.setString(15, setuLog.getException());
                    pstmt.setString(16, setuLog.getResultType());
                    pstmt.setString(17, setuLog.getXtLrsj());
                    pstmt.executeUpdate();
                    pstmt.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
